package com.example.demo.config.login;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * fileName:MyCredentialsMatcher
 * description:
 * author: LJV
 * createTime:2022/4/8 14:09
 * version:1.0.0
 */
public class MyCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken tokenResolve = (UsernamePasswordToken) token;
        //获得用户输入的密码:(可以采用加盐(salt)的方式去检验)
        String tokenPwd = new String(tokenResolve.getPassword());
        //获得用户名
        String username = tokenResolve.getUsername();
        //获得数据库中的密码
        String infoPwd =(String) info.getCredentials();
//        SimpleAuthenticationInfo saInfo = (SimpleAuthenticationInfo)info;
        ByteSource salt = ByteSource.Util.bytes(username);
        tokenPwd = new SimpleHash("sha-1", //加密方式
                tokenPwd,//密码原值
                salt,//盐值
                916//加密次数
        ).toString();
        //调用当前类重写的equals方法来对比两个password是否一致，返回对比结果
        return super.equals(tokenPwd, infoPwd);
    }

}
